//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.ServiceBus.Samples.ClientAccessPolicyPublisher
{
    using System;

    /// <summary>
    /// Writes to the <see cref="System.Console"/> using one of the specified <see cref="System.ConsoleColor"/>s
    /// </summary>
    public static class ColorConsole
    {
        private static object consoleLock = new object();

        /// <summary>
        /// Writes the value to the console in the specified color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="value">The value.</param>
        public static void Write(ConsoleColor color, object value)
        {
            lock (consoleLock)
            {
                var previousColor = Console.ForegroundColor;
                Console.ForegroundColor = color;

                Console.Write(value);

                Console.ForegroundColor = previousColor;
            }
        }

        /// <summary>
        /// Writes the specifed values to the console in the specified color using the given format.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="format">The format to use when presenting the arguments.</param>
        /// <param name="args">The arguments.</param>
        public static void Write(ConsoleColor color, string format, params object[] args)
        {
            lock (consoleLock)
            {
                var previousColor = Console.ForegroundColor;
                Console.ForegroundColor = color;

                Console.Write(format, args);

                Console.ForegroundColor = previousColor;
            }
        }

        /// <summary>
        /// Writes the value to the console in the specified color followed by an Environment.NewLine.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="value">The value.</param>
        public static void WriteLine(ConsoleColor color, object value)
        {
            lock (consoleLock)
            {
                var previousColor = Console.ForegroundColor;
                Console.ForegroundColor = color;

                Console.WriteLine(value);

                Console.ForegroundColor = previousColor;
            }
        }

        /// <summary>
        /// Writes the specifed values to the console in the specified color using the given format followed by an Environment.NewLine.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="format">The format to use when presenting the arguments.</param>
        /// <param name="args">The arguments.</param>
        public static void WriteLine(ConsoleColor color, string format, params object[] args)
        {
            lock (consoleLock)
            {
                var previousColor = Console.ForegroundColor;
                Console.ForegroundColor = color;

                Console.WriteLine(format, args);

                Console.ForegroundColor = previousColor;
            }
        }
    }
}
